package com.lz.ht.controller;

import com.lz.ht.model.Member;
import com.lz.ht.model.Room;
import com.lz.ht.page.PageModel;
import com.lz.ht.result.Result;
import com.lz.ht.service.MemberService;
import com.lz.ht.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName: MemberController
 * @Description:
 * @Author: CHonghao
 * @Date: 2021/7/10 8:53
 * @Version: V1.0
 **/
@Slf4j
@Controller
public class MemberController {

    @Autowired
    private MemberService memberServiceImpl;

    @RequestMapping(value = "/member/list", method = {RequestMethod.GET})
    public String member_list() throws Exception {
        return "member/member_list";
    }


    @RequestMapping(value = "/member/list", method = {RequestMethod.POST})
    @ResponseBody
    public PageModel list(Member member, PageModel<Member> page) throws Exception {
        page.init();
        List<Member> list = memberServiceImpl.findPageList(page, member);
        long count = memberServiceImpl.findCount(member);
        page.packData(count, list);
        return page;
    }

    @RequestMapping(value = "/member/add", method = {RequestMethod.GET})
    public String addInit(Member member, Model model) {
        return "member/member_add";
    }

    @RequestMapping(value = "/member/add", method = {RequestMethod.POST})
    @ResponseBody
    public Result add(Member member) {
        memberServiceImpl.add(member);
        return Result.genSuccessResult();
    }

    @RequestMapping(value = "/member/update",method = {RequestMethod.GET})
    public String updateInit(Member member, Model model){
        member = memberServiceImpl.findById(member.getMid());
        model.addAttribute("member",member);
        return "member/member_update";
    }

    @RequestMapping(value = "/member/update",method = {RequestMethod.POST})
    @ResponseBody
    public Result update(Member member){
        memberServiceImpl.updateById(member);
        return Result.genSuccessResult();
    }
    @RequestMapping(value = "/member/delete",method = {RequestMethod.POST})
    @ResponseBody
    public Result delete(Member member){
        memberServiceImpl.deleteById(member.getMid());
        return Result.genSuccessResult();
    }
}
